﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Framework.Serializer
{
    /// <summary>
    /// 序列化的扩展方法
    /// </summary>
    public static class ExtensionJson
    {
        static readonly JsonSerializer serializer = new JsonSerializer();

        #region 序列化
        /// <summary>
        /// 序列化成String
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static String SToStringJson(this Object obj)
        {
            return serializer.Serialize(obj);
        }
        /// <summary>
        /// 序列化到文件
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool SToFileJson(this Object obj, string filePath)
        {
            return serializer.SerializeToFile(obj, filePath);
        }
        #endregion

        #region 反序列化
        /// <summary>
        /// 反序列化为 T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T SFromStringJson<T>(this String obj)
        {
            return serializer.DeSerialize<T>(obj);
        }
        /// <summary>
        /// 从文件反序列化为 T 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static T SFromFileJson<T>(string filePath)
        {
            return serializer.DeSerializeFromFile<T>(filePath);
        }
        #endregion

    }


    ///// <summary>
    ///// 序列化的扩展方法
    ///// </summary>
    //public static class ExtensionNewtonsoftJson
    //{
    //    static NewtonsoftJsonSerializer serializer = new NewtonsoftJsonSerializer();

    //    #region 序列化
    //    /// <summary>
    //    /// 序列化成String
    //    /// </summary>
    //    /// <param name="obj"></param>
    //    /// <returns></returns>
    //    public static String SToStringNewtonsoftJson(this Object obj)
    //    {
    //        return serializer.Serialize(obj);
    //    }
    //    /// <summary>
    //    /// 序列化到文件
    //    /// </summary>
    //    /// <param name="obj"></param>
    //    /// <param name="filePath"></param>
    //    /// <returns></returns>
    //    public static bool SToFileNewtonsoftJson(this Object obj, string filePath)
    //    {
    //        return serializer.SerializeToFile(obj, filePath);
    //    }
    //    #endregion

    //    #region 反序列化
    //    /// <summary>
    //    /// 反序列化为 T
    //    /// </summary>
    //    /// <typeparam name="T"></typeparam>
    //    /// <param name="obj"></param>
    //    /// <returns></returns>
    //    public static T SFromStringNewtonsoftJson<T>(this String obj)
    //    {
    //        return serializer.DeSerialize<T>(obj);
    //    }
    //    /// <summary>
    //    /// 从文件反序列化为 T 
    //    /// </summary>
    //    /// <typeparam name="T"></typeparam>
    //    /// <param name="filePath"></param>
    //    /// <returns></returns>
    //    public static T SFromFileNewtonsoftJson<T>(string filePath)
    //    {
    //        return serializer.DeSerializeFromFile<T>(filePath);
    //    }
    //    #endregion

    //}


    //public static class ExtensionBson
    //{
    //    static BsonSerializer serializer = new BsonSerializer();

    //    #region 序列化
    //    /// <summary>
    //    /// 序列化为Byte[]
    //    /// </summary>
    //    /// <param name="obj"></param>
    //    /// <returns></returns>
    //    public static Byte[] SToBytesBson(this Object obj)
    //    {
    //        return serializer.Serialize(obj);
    //    }
    //    /// <summary>
    //    /// 序列化到文件
    //    /// </summary>
    //    /// <param name="obj"></param>
    //    /// <param name="filePath"></param>
    //    /// <returns></returns>
    //    public static bool SToFileBson(this Object obj, string filePath)
    //    {
    //        return serializer.SerializeToFile(obj, filePath);
    //    }
    //    #endregion

    //    #region 反序列化
    //    /// <summary>
    //    /// 反序列化为 T
    //    /// </summary>
    //    /// <typeparam name="T"></typeparam>
    //    /// <param name="obj"></param>
    //    /// <returns></returns>
    //    public static T SFromBytesBson<T>(this Byte[] obj)
    //    {
    //        return serializer.DeSerialize<T>(obj);
    //    }
    //    /// <summary>
    //    /// 从文件反序列化为 T 
    //    /// </summary>
    //    /// <typeparam name="T"></typeparam>
    //    /// <param name="filePath"></param>
    //    /// <returns></returns>
    //    public static T SFromFileBson<T>(string filePath)
    //    {
    //        return serializer.DeSerializeFromFile<T>(filePath);
    //    }
    //    #endregion
    //}

    public static class ExtensionProtoBuf
    {
        static readonly ProtoBufSerializer serializer = new ProtoBufSerializer();

        #region 序列化
        /// <summary>
        /// 序列化为Byte[]
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static Byte[] SToBytesProtoBuf(this Object obj)
        {
            return serializer.Serialize(obj);
        }
        /// <summary>
        /// 序列化到文件
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool SToFileProtoBuf(this Object obj, string filePath)
        {
            return serializer.SerializeToFile(obj, filePath);
        }
        #endregion

        #region 反序列化
        /// <summary>
        /// 反序列化为 T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T SFromBytesProtoBuf<T>(this Byte[] obj)
        {
            return serializer.DeSerialize<T>(obj);
        }
        /// <summary>
        /// 从文件反序列化为 T 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static T SFromFileProtoBuf<T>(string filePath)
        {
            return serializer.DeSerializeFromFile<T>(filePath);
        }
        #endregion

    }


    public static class ExtensionMongoBson
    {
        static readonly MongoBsonSerializer serializer = new MongoBsonSerializer();

        #region 序列化
        /// <summary>
        /// 序列化为Byte[]
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static Byte[] SToBytesMongoBson(this Object obj)
        {
            return serializer.Serialize(obj);
        }
        /// <summary>
        /// 序列化到文件
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool SToFileMongoBson(this Object obj, string filePath)
        {
            return serializer.SerializeToFile(obj, filePath);
        }
        #endregion

        #region 反序列化
        /// <summary>
        /// 反序列化为 T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T SFromBytesMongoBson<T>(this Byte[] obj)
        {
            return serializer.DeSerialize<T>(obj);
        }
        /// <summary>
        /// 从文件反序列化为 T 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static T SFromFileMongoBson<T>(string filePath)
        {
            return serializer.DeSerializeFromFile<T>(filePath);
        }
        #endregion

    }

    //public static class ExtensionMongoJson
    //{
    //    static readonly MongoJsonSerializer serializer = new MongoJsonSerializer();

    //    #region 序列化
    //    /// <summary>
    //    /// 序列化成String
    //    /// </summary>
    //    /// <param name="obj"></param>
    //    /// <returns></returns>
    //    public static String SToStringMongoJson(this Object obj)
    //    {
    //        return serializer.Serialize(obj);
    //    }
    //    /// <summary>
    //    /// 序列化到文件
    //    /// </summary>
    //    /// <param name="obj"></param>
    //    /// <param name="filePath"></param>
    //    /// <returns></returns>
    //    public static bool SToFileMongoJson(this Object obj, string filePath)
    //    {
    //        return serializer.SerializeToFile(obj, filePath);
    //    }
    //    #endregion

    //    #region 反序列化
    //    /// <summary>
    //    /// 反序列化为 T
    //    /// </summary>
    //    /// <typeparam name="T"></typeparam>
    //    /// <param name="obj"></param>
    //    /// <returns></returns>
    //    public static T SFromStringMongoJson<T>(this String obj)
    //    {
    //        return serializer.DeSerialize<T>(obj);
    //    }
    //    /// <summary>
    //    /// 从文件反序列化为 T 
    //    /// </summary>
    //    /// <typeparam name="T"></typeparam>
    //    /// <param name="filePath"></param>
    //    /// <returns></returns>
    //    public static T SFromFileMongoJson<T>(string filePath)
    //    {
    //        return serializer.DeSerializeFromFile<T>(filePath);
    //    }
    //    #endregion

    //}



}
